//
//  ReleaseDeliciousFoodController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/24.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "ReleaseDeliciousFoodController.h"
#import "ReleaseTextAndImageController.h"
#import "SelecteStyleOfCookingController.h"
#import "ReleaseDeliciousFoodTwoController.h"

@interface ReleaseDeliciousFoodController ()<UICollectionViewDelegate,UICollectionViewDataSource>
/**<#属性#>*/
@property (nonatomic,strong)UICollectionView*listView;
/**<#属性#>*/
@property (nonatomic,strong) UICollectionViewFlowLayout * layout;
/**数据源*/
@property (nonatomic,strong)NSMutableArray*dataArray;
/**标题*/
@property (nonatomic,strong)NSString*headerTitle;
/**菜系*/
@property (nonatomic,strong)NSString*selecteTitle;

@end

@implementation ReleaseDeliciousFoodController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavItems];
    [self createUI];
    [ReleaseFoodManage clearAction];
}
-(void)nextAction{
    NSMutableArray * images = [NSMutableArray array];//图片地址数组
    for (NSInteger i = 0 ; i < self.dataArray.count ; i++) {
        PhotoModel * model = self.dataArray[i];
        NSString * name = [ImageAndVideoSaveTool get_ONLYID];
        [images addObject:name];
        [ImageAndVideoSaveTool saveImageDocuments:model.highDefinitionImage imageName:name];
    }
    ReleaseFoodManage.images = images;
    ReleaseFoodManage.title = self.headerTitle;
    ReleaseFoodManage.styleOfCooking = self.selecteTitle;
    

    [self.navigationController pushViewController:[ReleaseDeliciousFoodTwoController new] animated:YES];

}
-(void)setNavItems{
    self.title = @"发布（1/3）";
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setBackgroundImage:IMAGE_NAMED(@"back") forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [leftBtn setTitleColor:UIColorFromRGB(0x273323) forState:UIControlStateNormal];
    leftBtn.titleLabel.font = SYSTEMFONT(16);
//    leftBtn.frame = CGRectMake(0, 0, 50, 30);
    [leftBtn sizeToFit];
    
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [rightBtn setTitleColor:UIColorFromRGB(0x959E97) forState:UIControlStateNormal];
    rightBtn.titleLabel.font = SYSTEMFONT(16);
    rightBtn.frame =CGRectMake(0, 0, 50, 30);

    kWeakSelf(weakSelf);
    [rightBtn addActionHandler:^{
        [weakSelf.view endEditing:YES];
        if (weakSelf.dataArray.count == 0) {
            ShowHubMessage(@"请上传图片");
            return ;
        }
        if (!StrValid(weakSelf.headerTitle)) {
            ShowHubMessage(@"请输入作品名称");
            return;
        }
        
        if (!StrValid(weakSelf.selecteTitle)) {
            ShowHubMessage(@"请选择菜系");
            return;
        }
        [weakSelf nextAction];
    }];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];

    
}
-(void)createUI{
    [self.view addSubview:self.listView];
    
}

#pragma mark ---UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return self.dataArray.count + 1;
    }
    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section != 0) {
        ReleaseDeliciousFoodCell * oneCell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(ReleaseDeliciousFoodCell.class) forIndexPath:indexPath];
        kWeakSelf(weakSelf);
        if (indexPath.section == 1) {
            oneCell.textFiled.hidden = NO;
            oneCell.leftLbale.hidden = YES;
            oneCell.moreImage.hidden = YES;
            oneCell.textFiled.text = self.headerTitle;
            oneCell.canceblock = ^(NSString * _Nonnull title) {
                weakSelf.headerTitle = title;
            };
        }else{
            oneCell.textFiled.hidden = YES;
            oneCell.leftLbale.hidden = NO;
            oneCell.moreImage.hidden = NO;
            oneCell.leftLbale.text = StrValid(self.selecteTitle) ? self.selecteTitle : @"选择菜系";
        }
        
        return oneCell;
    }
    
    ReleaseImageItem * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(ReleaseImageItem.class) forIndexPath:indexPath];
    if (indexPath.row == self.dataArray.count) {
        cell.isAdd = YES;
    }else{
        cell.model = self.dataArray[indexPath.row];
        cell.isAdd = NO;
        kWeakSelf(weakSelf);
        cell.deleBlck = ^{
            DLogFunc;
            [weakSelf.dataArray removeObjectAtIndex:indexPath.row];
            [weakSelf.listView reloadData];
        };
    }
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section != 0) {
        
        
        if (indexPath.section == 2) {
            SelecteStyleOfCookingController * vc  = [SelecteStyleOfCookingController new];
            [self.navigationController pushViewController:vc animated:YES];
            kWeakSelf(weakSelf);
            vc.callBlock = ^(NSString * _Nonnull title) {
                weakSelf.selecteTitle = title;
                [weakSelf.listView reloadData];
            };
        }
        
        
        return;
    }
    
    if (indexPath.row == self.dataArray.count) {
        kWeakSelf(weakSelf);
//        [PhotosManager showPhotosManager:self withMaxImageCount:10 withAlbumArray:^(NSMutableArray<PhotoModel *> *albumArray) {
//            DLog(@"%@", albumArray);
//            [weakSelf.dataArray addObjectsFromArray:albumArray];
//            [weakSelf.listView reloadData];
//        }];
        [PhotosManager showPhotosManager:self withMaxImageCount:9 withAlbumArray:^(NSMutableArray<PhotoModel *> * _Nonnull albumArray) {
            DLog(@"%@", albumArray);
            [weakSelf.dataArray addObjectsFromArray:albumArray];
            [weakSelf.listView reloadData];
        } withCameraImage:^(UIImage * _Nonnull cameraImage) {
            PhotoModel * model = [[PhotoModel alloc] init];
            model.highDefinitionImage = cameraImage;
            [weakSelf.dataArray addObject:model];
            [weakSelf.listView reloadData];
        }];
    }
}
#pragma mark  定义每个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        CGFloat width = (KScreenWidth - 3)/4;
        return CGSizeMake(width, width);
    }
    return CGSizeMake(KScreenWidth, 60);
}
// 列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}
// 行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}

#pragma mark ---懒加载
-(UICollectionView *)listView{
    if (!_listView) {
        self.layout= [[UICollectionViewFlowLayout alloc] init];
        _listView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-K_TopHeight) collectionViewLayout:self.layout];
        _listView.backgroundColor=KWhiteColor;
        _listView.delegate=self;
        _listView.dataSource = self;
        _listView.scrollEnabled = YES;
        [_listView registerClass:[ReleaseImageItem class] forCellWithReuseIdentifier:NSStringFromClass(ReleaseImageItem.class)];
        [_listView registerClass:[ReleaseDeliciousFoodCell class] forCellWithReuseIdentifier:NSStringFromClass(ReleaseDeliciousFoodCell.class)];

    }
    return _listView;
}
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray =[[NSMutableArray alloc] init];
    }
    return _dataArray;
}

@end


@interface ReleaseDeliciousFoodCell ()<UITextFieldDelegate>

@end


@implementation ReleaseDeliciousFoodCell

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}
-(void)createUI{
    
    self.moreImage = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"more")];
    
    [self addSubview:self.textFiled];
    [self addSubview:self.leftLbale];
    [self addSubview:self.moreImage];
    
    [self.textFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(16);
        make.bottom.right.mas_equalTo(-16);
    }];
    
    [self.leftLbale mas_makeConstraints:^(MASConstraintMaker *make) {
       make.left.top.mas_equalTo(16);
       make.bottom.mas_equalTo(-16);
    }];
    
    [self.moreImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-16);
    }];
}
#pragma mark UITextFieldDelegate
-(void)textFieldDidEndEditing:(UITextField *)textField{
    BlockSafeRun(self.canceblock,textField.text);
}
-(UITextField*)textFiled{
    if(!_textFiled){
        _textFiled=[UITextField new];
        _textFiled.placeholder = @"请输入你作品名称";
        _textFiled.delegate = self;
    }
    return _textFiled;
}
-(UILabel*)leftLbale{
    if(!_leftLbale){
        _leftLbale=[UILabel new];
        _leftLbale.textColor = UIColorFromRGB(0x959E97);
        _leftLbale.font =SYSTEMFONT(18);
        _leftLbale.text = @"选择菜系";
    }
    return _leftLbale;
}
@end
